<?php


class Images extends CActiveRecord
{
    public $delete;

	private $image, $nameFormat, $width, $height;

    public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{images}}';
	}

	public function rules()
	{
		return array(
			array('image_file_id, image_name, image_src', 'required'),
			array('image_file_id', 'numerical', 'integerOnly'=>true),
			array('image_name, image_src', 'length', 'max'=>128),
			array('id, image_file_id, image_name, image_src', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'image_file_id' => 'Image File',
			'image_name' => 'Image Name',
			'image_src' => 'Image Src',
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('image_file_id',$this->image_file_id);
		$criteria->compare('image_name',$this->image_name,true);
		$criteria->compare('image_src',$this->image_src,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function resize($img, $width=0, $height=0)
    {
        if(!$img || !$width || !$height) return false;
        $this->image=$img;
        $this->width=$width;
        $this->height=$height;
        $this->nameFormat=array(
            'beforeName'=>'', //time().'-',
            'afterName'=>'-'.$width.'-'.$height,
        );
        $name=$this->formatedName;
        $params=array(
            'range'=>20,
            'accuracy'=>80,
            'path'=>$this->path,
            'file'=>$this->path.DIRECTORY_SEPARATOR.$name,
            'separator'=>'-',
            'firstNumCheck'=>0,
        );
        if(!$file=Yii::app()->image->set($img, $params)->resize($this->width, $this->height)){
            return false;
        }
        $this->attributes=array(
            'image_file_id'=>$img->id,
            'image_name'=>$img->name,
            'image_src'=>$this->baseSrc.DIRECTORY_SEPARATOR.$name,
        );
        if($file=='EXIST') return $this;

        if($this->save()){
            return $this;
        }

        return false;
    }

    public function getFileId()
    {
        return $this->image_file_id;
    }

    public function getName()
    {
        return $this->image_name;
    }

    public function getSrc()
    {
        return $this->image_src;
    }

    public function getFile()
    {
        $file=new Files;
        return $file->findByPk($this->fileId);
    }

    public function getFileName()
    {
        return $this->file->fileName;
    }

    public function getBaseSrc()
    {
        $dir=new Directories;
        $dir=$dir->findByPk(1);
        return $dir->src.DIRECTORY_SEPARATOR.'tmp';
    }

    public function getPath()
    {
        $dir=new Directories;
        return $dir->basePath.DIRECTORY_SEPARATOR.'tmp';
    }

    protected function getFormatedName()
    {
        return $this->nameFormat['beforeName'].$this->image->name.$this->nameFormat['afterName'].'.'.$this->image->exp;
    }
}